package gsd.gaudi.common;


import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.ActionListener;
import java.lang.reflect.Method;
import java.util.EventListener;
import java.util.HashMap;

import javax.accessibility.AccessibleAction;
import javax.accessibility.AccessibleContext;
import javax.swing.JMenu;


public class GaudiGuiHierarchy {

	private HashMap<String,Component> registerK2W;
	private HashMap<Component,String> registerW2K;
	
	public HashMap<String, Component> getRegisterK2W() {
		return registerK2W;
	}

	public void setRegisterK2W(HashMap<String, Component> registerK2W) {
		this.registerK2W = registerK2W;
	}

	public HashMap<Component, String> getRegisterW2K() {
		return registerW2K;
	}

	public void setRegisterW2K(HashMap<Component, String> registerW2K) {
		this.registerW2K = registerW2K;
	}

	public GaudiGuiHierarchy(){
		this.registerK2W = new HashMap<String,Component>();
		this.registerW2K = new HashMap<Component,String>();
	}
	
	public void processGUI(){
		Component[] rootWindows = Window.getWindows();
		this.processWidgetChildren(rootWindows, "0");
	}
	
	public void processWidget(Component w, String index){
		
		if(this.registerWidget(w, index)){

			if(w instanceof Container){
				Container c = (Container)w;
				this.processWidgetChildren(c.getComponents(), index);
			}
			if(w instanceof JMenu){
				JMenu c = (JMenu)w;
				this.processWidgetChildren(c.getMenuComponents(), index);
			}
		}
	}
	
	public void processWidgetChildren(Component[] array, String pindex){
		
		if(array == null)
			return;
		
		int index = 0;
			
		for(Component w : array){
			this.processWidget(w, pindex + "/" + String.valueOf(index));
			index++;
		}
	}
	

	public boolean registerWidget(Component w, String s){
		
		if(w == null)
			return false;
		
		if(this.registerK2W.get(s) != null)
			return false;
		
		System.out.println("[Gaudi]-->Registering: " + w.getClass().getCanonicalName() + " with id: " + s);
		this.registerK2W.put(s,w);
		this.registerW2K.put(w,s);
		return true;
	}
	
	public void updateWidget(Component w, String index) {
		
		//System.out.println("[Gaudi]-->Updating: " + w.getClass().getCanonicalName() + " with id: " + index);
		this.registerW2K.remove(index);
		this.registerK2W.put(index,w);
		this.registerW2K.put(w,index);
		
		if(w instanceof Container){
			Container c = (Container)w;
			this.updateWidgetChildren(c.getComponents(), index);
		}
		if(w instanceof JMenu){
			JMenu c = (JMenu)w;
			this.updateWidgetChildren(c.getMenuComponents(), index);
		}
		
	}
	
	public void updateWidgetChildren(Component[] array, String pindex){

		if(array == null)
			return;

		int index = 0;

		for(Component w : array){
			this.updateWidget(w, pindex + "/" + String.valueOf(index));
			index++;
		}
	}
	
	public String findWidgetId(Component w){
		String id = this.registerW2K.get(w);
		return id;
	}
	
	public Component findWidget(String id){
		Component w = this.registerK2W.get(id);
		return w;
	}

	
}
